package com.zx.deploy.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.jdbc.ScriptRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.zx.deploy.common.exception.BusinessException;
import com.zx.deploy.common.utils.CommonUtil;
import com.zx.deploy.common.utils.DatetimeUtil;
import com.zx.deploy.common.utils.RuntimeUtil;
import com.zx.deploy.config.DeployConfig;
import com.zx.deploy.dao.BackDbDao;
import com.zx.deploy.dao.PermissionDao;
import com.zx.deploy.model.DatabaseConfig;
import com.zx.deploy.model.SpeedProgress;
import com.zx.deploy.service.DataBaseService;

@Service("JdbcDataBaseService")
public class JdbcDataBaseServiceImpl implements DataBaseService {

	@Value("${spring.datasource.username}")
	private String userName;

	@Value("${spring.datasource.password}")
	private String password;

	@Value("${spring.datasource.url}")
	private String datasource_url;

	@Value("${spring.datasource.driver-class-name}")
	private String driver;

	/**
	 * 环境变量
	 */
	@Autowired
	protected DeployConfig config;

	@Autowired
	protected BackDbDao backDbDao;

	@Override
	public SpeedProgress backupDatabase(DeployConfig config) {
		String path = config.backups_database_path;
		DatabaseConfig dbconfig = new DatabaseConfig(datasource_url, userName, password, "mysql", driver,config.database_name);
		SpeedProgress sp = new SpeedProgress(100, "备份数据库");
		try {
			backDbDao.readDataToFile(null, path, config, dbconfig);
			sp.setInfo("备份数据库成功");
		} catch (Exception e) {
			throw new BusinessException("备份数据库失败:" + e.getMessage());
		}
		return sp;
	}

	@Override
	public SpeedProgress rollbackDatabase(String callBackSql, DeployConfig config) {
		SpeedProgress sp = new SpeedProgress(100, "恢复数据库");
		try {
			PermissionDao per = new PermissionDao();
			String back_file_path = config.backups_database_path + callBackSql;
			List<String> sqls=new ArrayList<>();
			sqls.add(back_file_path);
			DatabaseConfig dbconfig = new DatabaseConfig(datasource_url, userName, password, "mysql", driver,config.database_name);
			per.runScript(sqls, dbconfig);
			sp.setInfo("恢复数据库成功");
		} catch (Exception e) {
			throw new BusinessException("恢复数据库失败:" + e.getMessage());
		}
		return sp;
	}

	@Override
	public void executeSqlIgnoreError(DeployConfig config, SpeedProgress speed) {
//		ScriptRunner runner=new ScriptRunner(connection);
//		 Resources.setCharset(Charset.forName("GBK")); 
//		    runner.setLogWriter(null);
//		runner.runScript(Resources.getResourceAsReader("sql/CC21-01.sql"));  
//    
//        runner.closeConnection();  
//        conn.close();  
	}

	@Override
	public void executeSql(DeployConfig config, SpeedProgress speed) {
		try {
			String script_path = config.soft_file_path + config.soft_unzip_name + File.separator + config.soft_sql_name
					+ File.separator;
			File file = new File(script_path);
			File[] files = file.listFiles();
			List<String> sqlNames = new ArrayList<>();
			if (files != null && files.length > 0) {
				Arrays.asList(files).stream().forEach(t -> sqlNames.add(script_path+t.getName()));
			}
			PermissionDao per = new PermissionDao();
			DatabaseConfig dbconfig = new DatabaseConfig(datasource_url, userName, password, "mysql", driver,config.database_name);
			per.runScript(sqlNames, dbconfig);
		} catch (BusinessException e) {
			throw new BusinessException( e.getMessage());
		} catch (Exception e) {
			throw new BusinessException( "执行脚本异常:"+e.getMessage());
		}
	}

}
